package cn.buddha.jec.flowable.handler.business;

import cn.buddha.jec.util.TongSpringContextUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName BusinessExtraFactory
 * @Author 友野浩二
 * @Date 2020/4/20 16:37
 * @Description 业务扩展工厂
 * @Version 1.0
 */
public class BusinessExtraFactory {

    /**
     * @return java.util.List<T>
     * @Author 友野浩二
     * @Description // 根据接口获取所有有效的实现类,并对其进行排序
     * @Date 2020/4/20 20:21
     * @Param [clazz]
     */
    public static <T extends IBusinessExtraHandler> List<T> getEffectiveExtra(Class<T> clazz) {
        // 根据接口获取所有实现类
        Map<String, T> extraMap = TongSpringContextUtils.getBeansOfType(clazz);
        // 将map的value(所有实现类)转换为list集合
        ArrayList<T> extraList = new ArrayList<>(extraMap.values());
        // 按照getOrder()方法对服务流程前/后置的组件进行排序(默认升序)
//        Collections.sort(extraList, (o1, o2) -> {return o1.getOrder()-o2.getOrder();});
//        Collections.sort(extraList, Comparator.comparingInt(IBusinessExtraHandler::getOrder));
        extraList.sort(Comparator.comparingInt(T::getOrder));
        // 剔除服务流程前/后置中未生效的组件
//        return extraList.stream().filter(t -> {return t.isEffective();}).collect(Collectors.toList());
        return extraList.stream().filter(T::isEffective).collect(Collectors.toList());
    }

}
